package commonClass;

import java.util.ArrayList;

import net.sf.json.JSONArray;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import Modle.disease.SymptomsCategorie;

public class ADAqueryNoIsAnatomySymptomsByAnimalIDActionoffz2 implements
		IAnimalDoctorAction {

	@Override
	public String object2json(ArrayList<Object> list) {

		String jsonString = "";
		//
		// Integer j = (Integer) list.get(0);
		// "animalID=01371054603"
		jsonString = "animalID=" + (String) list.get(0);
		return jsonString;
	}

	@Override
	public ArrayList<Object> json2object(String json) {

		ArrayList<Object> list = null;

		// Integer i = new Integer(10);
		// list.add(i);
		if (!jsonIsData(json))
			return null;

		list = getListFromJson(json);

		return list;
	}

	private ArrayList<Object> getListFromJson(String json) {

		ArrayList<Object> list = new ArrayList<Object>();

		org.json.JSONArray jsonObjs = null;
		try {
			jsonObjs = new JSONObject(json).getJSONArray("symptomsCategorie");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String segJson = "";
		Log.v("000000", "getListFromJson");
		for (int i = 0; i < jsonObjs.length(); i++) {
			JSONObject jsonObj = ((JSONObject) jsonObjs.opt(i));
//			segJson = new Gson().toJson(jsonObj);
			SymptomsCategorie obj = json2SymptomsCategorie(jsonObj);
			list.add(obj);
		}

		return list;

		/*
		 * int id = jsonObj.getInt("id"); String name =
		 * jsonObj.getString("name"); String gender =
		 * jsonObj.getString("gender"); s += "ID号"+id + ", 姓名：" + name + ",性别："
		 * + gender+ "\n" ;
		 */

	}

	private boolean jsonIsData(String json) {
		// String json =
		// "{'response':{'data':[{'address':'南京市游乐园','province':'江苏','district':'玄武区','city':'南京'}]},'status':'ok'}";

		JSONObject jsonObj;
		String result = "";
		try {
			jsonObj = new JSONObject(json);
			result = jsonObj.getString("code");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return (result.equals("success"));
	}

	private SymptomsCategorie json2SymptomsCategorie(JSONObject jsonObj) {
		SymptomsCategorie obj = new SymptomsCategorie();

		// new Gson().fromJson(segJson,SymptomsCategorie.class);

		try {
//			JSONObject jsonObj = new JSONObject(segJson);

			obj.setAnimalID(jsonObj.getString("animalID"));
			obj.setAnimalName((jsonObj.getString("animalName")));
			obj.setIsAnatomySymptoms((jsonObj.getInt("isAnatomySymptoms")));
			obj.setIsBasicSymptom((jsonObj.getInt("isBasicSymptom")));
			obj.setSymptomsCategorieID((jsonObj
					.getString("symptomsCategorieID")));
			obj.setSymptomsCategorieName((jsonObj
					.getString("symptomsCategorieName")));

			Log.v("000000", "SymptomsCategorie:" + obj.toString());
			return obj;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

}
